
<!--generated with sswg-->
<style>
    html {max-width: 100%; margin: auto; color: #333333;}
    a.button {padding: 15px 32px; background-color: #555; border-radius: 2em; border-width: 0px; text-decoration: none; color: white; font-size: 25.0px; line-height: 2.5em;}
    a.button:hover {background-color: #777}
    a.button_big {padding: 0.5em; background-image: linear-gradient(to top, #427b0e, #9ba97d); background-color: lightgray; background-blend-mode: multiply; border-radius: .75em; border-width: 0px; text-decoration: none; min-width: 150px; max-width: 150px; min-height: 150px; max-height: 150px; display: inline-block; vertical-align: top; margin: 4px 4px 10px 4px; color: white; font-size: 25.0px; background-size: auto 100%; background-position-x: center;}
    a.button_big:hover {background-color: white; color: #e6d23f; text-decoration: underline;}
    mark {background: #ccff99;}
    span {background-color: rgba(0, 0, 0, 0.55); padding: .1em; line-height: 1.35em;}
    img {max-width: 100%; vertical-align: top;}
    .code_block {background-color: whitesmoke; padding: 10px; margin: 0; font-family: monospace; font-size: 20; font-weight: normal; white-space: pre;}

</style>
<html>
<left>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> entity basics</title>
<br>

<div style="text-align: center;max-width: 1000px; margin: auto;font-family: arial;">
<div style="font-size: 100.0px;font-weight: bold;">
Entity Basics<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
<br>
<div style="text-align: left;">
<div style="font-size: 40.0px;font-weight: bold;">
<div id="What is an Entity?"/><br>
What is an Entity?<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;An entity is a thing in the world, and is the "god class" in ursina.<br>
&nbsp;&nbsp;&nbsp;&nbsp;It's like a GameObject in Unity or an Actor in Unreal.<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;It can have a position, rotation, and scale.<br>
&nbsp;&nbsp;&nbsp;&nbsp;It can have a model, texture and color.<br>
&nbsp;&nbsp;&nbsp;&nbsp;It can have an update function, input function and scripts.<br>
&nbsp;&nbsp;&nbsp;&nbsp;For an overview of all its attributes and functions, see <a href="https://www.ursinaengine.org/cheat_sheet.html#Entity">https://www.ursinaengine.org/cheat_sheet.html#Entity</a><br>
<br>
<br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Setting a model"/><br>
Setting a model<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;There are several included models like 'quad', 'plane', 'cube' and 'sphere',<br>
&nbsp;&nbsp;&nbsp;&nbsp;but you can also use your own.<br>
&nbsp;&nbsp;&nbsp;&nbsp;To do that simply give the base name of your model and it will glob/search for<br>
&nbsp;&nbsp;&nbsp;&nbsp;the model and take the first one it finds.<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Supported file types are:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* obj<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* bam (binary format)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* blend (gets auto converted to an obj)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* ursinamesh (custom human readable format identical to how you'd make a Mesh in code)<br>
<br>
<div class="code_block" style="margin-left: 1em;"><font color="olive">Entity</font>(model=<font color="green">'name_of_your_model'</font>)<br></div><br>
<br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Texture"/><br>
Texture<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Setting a texture is similar to setting a model. Just give it a name in this case as well:<br>
<br>
<div class="code_block" style="margin-left: 1em;">e1 = <font color="olive">Entity</font>(model=<font color="green">'cube'</font>, texture=<font color="green">'texture_name'</font>)<br><br><font color="gray"># other ways are:</font>
e2 = <font color="olive">Entity</font>(model=<font color="green">'cube'</font>, texture=e1.texture) <font color="gray"># or set it to another Texture</font><br>e3 = <font color="olive">Entity</font>(model=<font color="green">'cube'</font>, texture=PIL.Image.new(mode="RGBA", size=(854,480))) <font color="gray"># set a PIL texture</font><br>e4 = <font color="olive">Entity</font>(model=<font color="green">'cube'</font>, texture=<font color="green">'movie_name.mp4'</font>) <font color="gray"># set video texture</font><br></div><br>
&nbsp;&nbsp;&nbsp;&nbsp;For 2d graphics there's also the Sprite class, which is simply an Entity with a<br>
&nbsp;&nbsp;&nbsp;&nbsp;'quad' model and scale&nbsp;&nbsp;set to automatically fit the size and aspect ratio of the texture.<br>
<br>
<div class="code_block" style="margin-left: 1em;">s = Sprite(<font color="green">'texture_name'</font>)<br>print(s.aspect_ratio)<br></div><br>
<br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Setting the color"/><br>
Setting the color<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
<div class="code_block" style="margin-left: 1em;">e.color = color.red <font color="gray"># set it to a color in the color module</font><br><br>e.color = color.color(120, .5, .5) <font color="gray"># hsv color</font><br>e.color = color.rgb(.8, .1, 0) <font color="gray"># rgb color</font><br>e.color = e.color.tint(.1) <font color="gray"># tint the color</font><br>e.color = color.random_color() <font color="gray"># set it to a random color</font><br>e.color = lerp(color.red, color.green, .5) <font color="gray"># set it to a color half way between red and green</font><br></div><br>
<br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Moving"/><br>
Moving<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;You can move an entity by setting the attributes:<br>
&nbsp;&nbsp;&nbsp;&nbsp;'position', 'x', 'y', 'z', 'world_position', 'world_x', 'world_y', 'world_z'<br>
<br>
<div class="code_block" style="margin-left: 1em;"><font color="gray"># you can give it a position when you create i, or you can omit it and set it later</font>
e = <font color="olive">Entity</font>(position=(0,0,0))<br><font color="gray"># set position relative to parent entity:</font>
e.position = Vec3(0,0,0)<br>e.position = Vec2(0,0)<br>e.position = (0,0,0)<br>e.position = (0,0)<br><br><font color="gray"># shortcuts for individual axes:</font>
e.x = 0<br>e.y = 0<br>e.z = 0<br><br><font color="gray"># set position relative to the world:</font>
 e.world_position = (0,0,0)<br></div><br>
&nbsp;&nbsp;&nbsp;&nbsp;See <a href="https://www.ursinaengine.org/coordinate_system.html">https://www.ursinaengine.org/coordinate_system.html</a> for more<br>
&nbsp;&nbsp;&nbsp;&nbsp;information on how the coordinate system works.<br>
<br>
<br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Rotating"/><br>
Rotating<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
<div class="code_block" style="margin-left: 1em;">e.rotation = (0,0,0)<br>e.rotation_y = 90<br></div><br>
&nbsp;&nbsp;&nbsp;&nbsp;The look_at() method can also be for pointing an entity at something:<br>
<br>
<div class="code_block" style="margin-left: 1em;">other entity = <font color="olive">Entity</font>(position=(10,1,8))<br><br>e.look_at(other_entity) <font color="gray"># make z-axis(forward) point at other_entity</font><br>e.look_at(other_entity, axis=<font color="green">'up'</font>) <font color="gray"># optinally define which axis</font><br></div><br>
<br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Scaling"/><br>
Scaling<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Change the size of the entity by setting the scale attribute:<br>
<br>
<div class="code_block" style="margin-left: 1em;">e = <font color="olive">Entity</font>(model=<font color="green">'cube'</font>, scale=(3,1,1))<br></div><br>
<br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Update"/><br>
Update<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;An Entity's update method will be called automatically:<br>
<br>
<div class="code_block" style="margin-left: 1em;">e = <font color="olive">Entity</font>()<br><font color="purple">def</font> my_update():<br>    e.x += 1 * time.dt <font color="gray"># dt is short for delta time, the duration since the last frame.</font><br><br>e.update = my_update<br></div><br>
&nbsp;&nbsp;&nbsp;&nbsp;Or you can implement it by inheriting the Entity class:<br>
<br>
<div class="code_block" style="margin-left: 1em;">class Player(<font color="olive">Entity</font>):<br>    <font color="purple">def</font> update(self):<br>        self.x += 1 * time.dt<br></div><br>
<br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Input"/><br>
Input<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;The same goes for the input method:<br>
<br>
<div class="code_block" style="margin-left: 1em;">class Player(<font color="olive">Entity</font>):<br>    <font color="purple">def</font> input(self, key):<br>        if key == <font color="green">'w'</font>:<br>            self.position += self.forward<br><br>        if key == <font color="green">'d'</font>:<br>            self.animate(<font color="green">'rotation_y'</font>, self.rotation_y + 90, duration=.1)<br><br>        if key == <font color="green">'a'</font>:<br>            self.animate(<font color="green">'rotation_y'</font>, self.rotation_y - 90, duration=.1)<br></div><br>
<br>
<div style="font-size: 40.0px;font-weight: bold;">
<div id="Mouse Input"/><br>
Mouse Input<br>
<div style="font-size: 20.0px;font-weight: normal;">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Entities can react to the mouse as long as they have collider.<br>
&nbsp;&nbsp;&nbsp;&nbsp;Buttons will have one by default, but you can also assign one.<br>
<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;To get the entity currently under the mouse:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mouse.entity<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;To check if enity(self) is hovered:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.hovered<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Assign on_click() to make it get called when you click on the entity<br>
&nbsp;&nbsp;&nbsp;&nbsp;Assign on_double_click() to make it get called when you double click on the entity.<br>
<br>

</html>